package com.hzhc.controller.sysmanage;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.SysYzsRole;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysYzsRoleMenuService;
import com.hzhc.service.SysYzsRoleService;
import com.hzhc.utils.ResultUtil;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/4/12 10:28
 * @description SysYzsRoleController
 */
@RestController
@RequestMapping("/sysYzsRole")
@RequiredArgsConstructor
public class SysYzsRoleController extends BaseController {

    private final SysYzsRoleService sysYzsRoleService;


    private final SysYzsRoleMenuService sysYzsRoleMenuService;


    /**
     * getalllist 获取所有角色列表
     */
    @PostMapping("/getAllRoleList")
    public ResultUtil<List<SysYzsRole>> getUserList(HttpServletRequest request, @RequestBody SysYzsRole yzsRole) {
        List<SysYzsRole> list= sysYzsRoleService.getRoleList(yzsRole);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }


    /**
     * 角色列表getDataTableRolelist分页
     */
    @RequestMapping("/getDataTableRolelist")
    public ResultUtil<List<SysYzsRole>> getDataRoleList(HttpServletRequest request, @RequestBody JSONObject data) {
        int pageSize = data.getIntValue("pageSize");
        int pageIndex = data.getIntValue("pageIndex");
        String searchdata = data.getString("name");
        if (StrUtil.isBlank(searchdata)) {
            searchdata = null;
        }
        Page<SysYzsRole> page = new Page<>(pageIndex,pageSize);
        List<SysYzsRole> sysYzsRoleList = sysYzsRoleService.getPageRole(page, searchdata);
        return ResultUtil.build(sysYzsRoleList,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 添加
     *
     * @param datas
     * @return
     */
    @RequestMapping("/saveRole")
    @SysTemLog(operModul = "系统管理-一站式角色管理添加功能", operType = "添加", operDesc = "一站式角色添加功能", modular = "PC")
    public ResultUtil<T> saveRole(@RequestBody SysYzsRole datas) {
        Boolean bool  = sysYzsRoleService.saveRole(datas);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    /**
     * 更新
     *
     * @param datas
     * @return
     */
    @RequestMapping("/updateRole")
    @SysTemLog(operModul = "系统管理-一站式角色管理修改角色", operType = "修改", operDesc = "一站式角色管理修改功能", modular = "PC")
    public ResultUtil<T> updateRole(@RequestBody SysYzsRole datas) {
        Boolean bool = sysYzsRoleService.updateRole(datas);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    /**
     * delete删除
     * @param id
     * @return
     */
    @RequestMapping("/delRole/{id}")
    @SysTemLog(operModul = "系统管理-一站式角色管理删除角色", operType = "删除", operDesc = "一站式角色管理删除功能", modular = "PC")
    public ResultUtil<T>  delRole(@PathVariable("id") Long id) {
        Boolean bool = sysYzsRoleService.delRole(id);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    /**
     *  获取当前角色的菜单id
     * @param roleId
     * @return
     */
    @RequestMapping("/grantRole/{roleId}")
    public ResultUtil<List<Long>> grantRole(@PathVariable("roleId") Long roleId) {
        List<Long> list= sysYzsRoleMenuService.getListIdsByRoleId(roleId);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 保存用户权限菜单
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/grantSaveRole")
    @SysTemLog(operModul = "系统管理-一站式角色管理修改一站式角色菜单", operType = "修改角色菜单", operDesc = "一站式角色管理修改一站式角色菜单功能", modular = "PC")
    public ResultUtil<T> grantSaveRole(@RequestBody JSONObject obj) {
        ArrayList<Integer> data = (ArrayList<Integer>) obj.get("data");
        Long roleid = obj.getLong("roleid");
        Boolean bool = sysYzsRoleMenuService.saveOrUpdateRoleMenu(data, roleid);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }


}
